package gitobject;
import repository.Repository;
import zlib.ZLibUtils;
import fileoperation.FileCreation;

import java.io.*;


public class GitObject implements Serializable{

    protected String fmt;                  //type of object
    protected String key;                  //key of object
    protected String mode;                 //mode of object
    public static String path = Repository.getGitDir() + File.separator + "objects";          //absolute path of objects
    protected String value;                //value of object

    public String getFmt(){
        return fmt;
    }
    public String getKey() { return key; }
    public String getMode(){
        return mode;
    }
    public String getPath() {
        return path;
    }
    public String getValue(){
        return value;
    }


    public GitObject(){}
    /**
     * Get the value(content) of file
     * @param file
     * @return String
     * @throws IOException
     */
    public static String getValue(File file) throws IOException {
        //创建一个fileReader对象
        FileReader fileReader = null;
        int readLen = 0;
        char[] buf = new char[1024];
        StringBuilder sb = new StringBuilder();
        try{
            fileReader = new FileReader(file);
            while((readLen=fileReader.read(buf))!=-1){
                sb.append(new String(buf,0,readLen));
            }
        }catch(IOException e){
            throw new FileNotFoundException();
            //e.printStackTrace();
        }finally {
            if(fileReader != null){
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }
    public static String getValue(String filePath) throws IOException {
        return getValue(new File(filePath));
    }
    /**
     * Serialize the object to the local repository.
     * @throws Exception
     */
    public void writeObject() throws Exception{
        /**
         * Add your code here.
         */
        String whereTo = path + File.separator + this.getKey().substring(0,2) + File.separator + this.getKey().substring(2);
        String tempDir = path + File.separator + this.getKey().substring(0,2);
        if(!new File(tempDir).exists()){
            //把key的前两位取出，创建文件夹
            FileCreation.createDirectory( path ,this.getKey().substring(0,2));
        }
        OutputStream op =  new FileOutputStream( whereTo );
        ObjectOutputStream ops =  new ObjectOutputStream(op);
        ops.writeObject(this);
        ops.close();
        op.close();
    }

    /**
     * Todo: Serialize the object and compress with zlib.
     * @throws Exception
     */
    public void compressWrite() throws Exception{
        /**
         * Add your code here.
         */
        String whereTo = path + File.separator + this.getKey().substring(0,2) + File.separator + this.getKey().substring(2);
        OutputStream os = null;
        try{
            os = new FileOutputStream(whereTo);
            ZLibUtils.compress(this.getValue().getBytes(),os);
        }catch(IOException e){
            e.printStackTrace();
        }finally {
            if(os != null) os.close();
        }
    }

    public static GitObject deserialize(String Id) {
        String absPath = path + File.separator + Id.substring(0,2) + File.separator + Id.substring(2);
        GitObject g  = null;
        try{
            FileInputStream is = new FileInputStream(absPath);
            ObjectInputStream os = new ObjectInputStream(is);
            g = (GitObject) os.readObject();
            os.close();
            is.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return g;
       }
}
